/**
 * @file snake.c
 * @author 林彦年 (1216840923@qq.com)
 * @brief 
 * @version 0.1
 * @date 2022-08-11
 * 
 * @copyright Copyright (c) 2022
 * 
 */

#include "game.h"


char map[80][80] = {0};



//果实图片数据指针
int *food_bmp;
//地图原始数据
char map_tmp[80][80];


//图片显示协议密文
char pic_protocol[18] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17};

//游戏进度标志
int gameStart = 0;
int gameOver = 0;
int exitgame = -1;

//当前最高分
unsigned char mvp = 0;

//蛇行走速度 每speed us走一步
unsigned int snake_speed = 130000;

//蛇链表读写锁
pthread_rwlock_t snakeRwlock;


list_t* listAddrHead;//TCP服务链表
list_t* listSnake[4];//所有用户蛇链表数组
list_t* foodList = NULL;//果实链表

struct sockaddr_in upd_client_addr[4];

/**
 * @brief 游戏参数初始化
 * 
 */
void game_arguement_init()
{
    //游戏进度标志初始化
    gameStart = 0;
    gameOver = 0;
    exitgame = -1;
    XPRINTF("%s 1\n",__FUNCTION__);
    //初始化地图数据
    map_data_proccess(map);
    XPRINTF("%s 2\n",__FUNCTION__);
    //拷贝地图原始数据
    memcpy(&map_tmp[0][0],&map[0][0],sizeof(map_tmp));

    XPRINTF("%s 3\n",__FUNCTION__);
    //生成20个果实
    for(int i=0; i<20; i++)
    {
        foodCreate();//果实生成函数
    }
    XPRINTF("%s 4\n",__FUNCTION__);
}




int main()
{
    //调试函数初始化
    initLog();
   
    XPRINTF("%s 1\n",__FUNCTION__);
    //创建蛇身读写锁
    pthread_rwlock_init(&snakeRwlock,NULL);

    //初始化游戏参数
    game_arguement_init();
    XPRINTF("%s 2\n",__FUNCTION__);
    //创建TCP服务线程
    pthread_t thread_tcp_communicate;
    pthread_create(&thread_tcp_communicate,NULL,tcp_server,NULL);
    pthread_detach(thread_tcp_communicate);
    XPRINTF("%s 3\n",__FUNCTION__);
    
    //阻塞该线程
    while (1);
    
    return 0;
}